import DTO.Token;
import DTO.Student;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@WebServlet(urlPatterns = {"/login/dgut"})
public class Servlet1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String string1 = null,string2=null;

        String token = req.getParameter("token");
        List<NameValuePair> form = new ArrayList<>();
        form.add(new BasicNameValuePair("token", token));
        form.add(new BasicNameValuePair("appid", "javaee"));
        form.add(new BasicNameValuePair("appsecret", "b3b52e43ccfd"));
        form.add(new BasicNameValuePair("userip", req.getLocalAddr()));
        HttpPost httppost_1 = new HttpPost("https://cas.dgut.edu.cn/ssoapi/v2/checkToken");
        UrlEncodedFormEntity uefEntity_1;
       try (CloseableHttpClient httpclient_1 = HttpClients.createDefault()) {
            uefEntity_1 = new UrlEncodedFormEntity(form, "UTF-8");
            httppost_1.setEntity(uefEntity_1);
            try (CloseableHttpResponse response_1 = httpclient_1.execute(httppost_1)) {
                HttpEntity entity_1 = response_1.getEntity();
                if (entity_1 != null) {
                    string1= EntityUtils.toString(entity_1, "UTF-8");
                }
            }
        }



        Jsonb json = JsonbBuilder.create();
        Token to = json.fromJson(string1,Token.class);

        String openid=to.getOpenid();
        String access_token1=to.getAccess_token();
        List<NameValuePair> form1= new ArrayList<>();

        form1.add(new BasicNameValuePair("access_token", access_token1));
        form1.add(new BasicNameValuePair("openid", openid));

        HttpPost httppost_2 = new HttpPost("https://cas.dgut.edu.cn/oauth/getUserInfo");
        UrlEncodedFormEntity uefEntity_2;
        try (CloseableHttpClient httpclient_2 = HttpClients.createDefault()) {
            uefEntity_2 = new UrlEncodedFormEntity(form1, "UTF-8");
            httppost_2.setEntity(uefEntity_2);
            try (CloseableHttpResponse response_2 = httpclient_2.execute(httppost_2)) {
                HttpEntity entity_2 = response_2.getEntity();
                if (entity_2 != null) {
                    string2= EntityUtils.toString(entity_2, "UTF-8");
                }
            }
        }
        Jsonb json_student=JsonbBuilder.create();
        Student student=json.fromJson(string2,Student.class);

        req.getSession().setAttribute("student",student);

        resp.sendRedirect("http://localhost:8080/infor.jsp");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}






